package com.dragon.www.haier.popupwindow;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.dragon.www.haier.R;
import com.dragon.www.haier.entity.DeviceInfo;
import com.dragon.www.haier.entity.Room;
import com.dragon.www.haier.net.NetHelper;
import com.dragon.www.haier.net.NetUtils;

import java.util.List;

/**
 * Created by liqp on 2016/6/17.
 */
public class AllPopWindow extends BasePopupWindow{
    private final static int allPopBackgroundColor = 0xD9009bf4;
    private ImageButton allUp;
    private ImageButton allDown;
    private ImageView imageBackAll;
    private List<Room> mRooms = null;                           //总的房间列表

    public AllPopWindow(Context context) {
        super(context);
    }
    public void showPopWindow(View view, int xOffsetToView, int yOffsetToView) {
        contentView = LayoutInflater.from(context).inflate(R.layout.all_view, null);
        super.showPopWindow(view, xOffsetToView, yOffsetToView);


        this.setBackgroundDrawable(new BitmapDrawable());
        GradientDrawable myGrad = (GradientDrawable) contentView.getBackground();
        myGrad.setColor(allPopBackgroundColor);

        imageBackAll = (ImageView) contentView.findViewById(R.id.image_back_all);
        allUp = (ImageButton) contentView.findViewById(R.id.all_up);
        allDown = (ImageButton) contentView.findViewById(R.id.all_down);
        allUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAllUp();
            }
        });
        allDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAllDown();
            }
        });
        imageBackAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();

            }
        });
    }

    public void setRooms(List<Room> rooms){
        this.mRooms = rooms;
    }

    public void setAllUp() {
        if (com.dragon.www.haier.util.Utils.isFastClick(500)) {
            return;
        }

        for (Room x : mRooms){
            NetHelper.sendall(context, String.valueOf(x.getRoomid()), 1, 1, new NetUtils.NetCallBack<DeviceInfo>() {
                @Override
                public void success(DeviceInfo rspData) {
                    if (rspData.getError() == 0) {
                        Log.e("全开", "操作成功");
                    } else {
                        String DD = rspData.getDescription();
                        Log.e("错误描述", DD);
                    }
                }

                @Override
                public void failed(String msg) {
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                }
            }, DeviceInfo.class);

        }
    }

    public void setAllDown() {
        if (com.dragon.www.haier.util.Utils.isFastClick(500)) {
            return;
        }
        for (Room x : mRooms){
            NetHelper.sendall(context, String.valueOf(x.getRoomid()), 1, 0, new NetUtils.NetCallBack<DeviceInfo>() {
                @Override
                public void success(DeviceInfo rspData) {
                    if (rspData.getError() == 0) {
                        Log.e("全开", "操作成功");
                    } else {
                        String DD = rspData.getDescription();
                        Log.e("错误描述", DD);
                    }
                }

                @Override
                public void failed(String msg) {
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                }
            }, DeviceInfo.class);

        }
    }
}
